package com.ajx.blog.service.impl;

import com.ajx.blog.entity.ContentType;
import com.ajx.blog.mapper.ContentTypeMapper;
import com.ajx.blog.service.api.ContentTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ContentTypeServiceImpl extends ServiceImpl<ContentTypeMapper, ContentType> implements ContentTypeService {

    @Autowired
    private ContentTypeMapper contentTypeMapper;

    @Override
    public List<ContentType> selectAllType() {
        return contentTypeMapper.selectList(null);
    }

    /**
     * @param {*}
     * @return {Map<String, Integer>} 类型和类型文章数量对应的map
     * 
     * @description: 查询各类型的文章数量
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Cacheable(cacheNames = "selectArticleNumByType", unless = "#result == null")
    @Override
    public Map<String, Integer> selectArticleNumByType() {
        Map<String, Integer> resultMap = new HashMap<>();
        List<ContentType> contentTypeList = contentTypeMapper.selectArticleNumByType();
        for (ContentType contentType : contentTypeList) {
            resultMap.put(contentType.getTypename(), contentType.getPid());
        }
        return resultMap;
    }

}
